package cn.edu.xjtu.android.emoji_warfare_android.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import cn.edu.xjtu.android.emoji_warfare_android.util.Gap;

/**
 * GameFrameView<br>
 * 游戏绘制板面
 * 
 * @author bill
 *
 */
public class GameFrameView extends SurfaceView implements SurfaceHolder.Callback, CheckDraw {

    private SurfaceHolder mHolder;

    Gap g;

    private OnDraw mOnDraw;

    private boolean showing;

    private boolean debug = false;

    public GameFrameView(Context context) {
        this(context, null);
    }

    public GameFrameView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GameFrameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initSurfaceHolder();
        if (debug) {
            g = new Gap("GameFrameView");
            g.mstart();
        }
    }

    private void initSurfaceHolder() {
        setZOrderOnTop(true);
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setFormat(PixelFormat.TRANSLUCENT);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (isInEditMode()) {
            // 绘制模式下无须绘制任何图像
        }
    }

    @SuppressLint("WrongCall")
    public void checkDraw() {
        Canvas canvas = null;
        try {
            canvas = mHolder.lockCanvas();
            if (canvas == null) {
                Log.w("GameFrameView", "GameFrameView got null canvas");
                return;
            }
            if (showing) {
                if (mOnDraw != null) {
                    canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
                    mOnDraw.onDraw(canvas);
                } else {
                    Log.w("GameFrameView", "GameFrameView got null on draw method");
                }
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            if (canvas != null) {
                mHolder.unlockCanvasAndPost(canvas);
            }
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        Log.d("GameFrameView", "GameFrameView surface changed");
    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        Log.d("GameFrameView", "GameFrameView surface created");
        showing = true;
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        Log.d("GameFrameView", "GameFrameView surface destroyed");
        showing = false;
    }

    @Override
    protected void onVisibilityChanged(View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        if (visibility == VISIBLE) {
            Log.d("GameFrameView", "GameFrameView visible");
        } else {
            Log.d("GameFrameView", "GameFrameView invisible");
        }
    }

    public void setOnDraw(OnDraw onDraw) {
        this.mOnDraw = onDraw;
    }

}
